package com.kc.sjwh.action;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Lxyybzzd;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.service.LxyybzzdService;

/**
 * 离席原因备注字典
 * @author zhouy
 *
 */
@Controller
@RequestMapping("lxyybzzd")
public class LxyybzzdAction extends BaseAction{
	static Logger logger = Logger.getLogger(JqxzAction.class);

	@Autowired
	LxyybzzdService lxyybzzdService;
	
	/**
	 * 打开列表页面
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String listPage(HttpServletRequest req){
		return "lxyybzzd/lxyyList";
	}
	
	/**
	 * 进入编辑页面
	 * @param req
	 * @param id
	 * @return
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id){
		req.setAttribute("act", id==null?"新增":"修改");
		Lxyybzzd one = new Lxyybzzd();
		if(null != id){
			one = lxyybzzdService.getOneInfo(id);
		}
		req.setAttribute("obj", one);
		return "lxyybzzd/lxyyForm";
	}
	
	/**
	 * 获得列表数据
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getData")
	public String getData(HttpServletRequest req, int pageSize, int pageNum){
		QueryParam p = new QueryParam(pageSize, pageNum);
		p.addSqlCondition(" t.sybz = 'Y' ");
		String zdname = req.getParameter("zdname");
		if(null != zdname){
			zdname = zdname.trim();
			if(!"".equals(zdname)){
				p.addSqlCondition(" t.bzmc like '%" + zdname + "%'");
			}
		}
		p.setOrder(" t.gxsj desc, t.bzbh asc");
		Page<Lxyybzzd> page = lxyybzzdService.getData(p);
		/*Table table = new Table();
		table.add(new Tr("编号", "备注名称", "时长限制(秒)", "操作员可用", "创建时间", "更新时间", "操作"));//表头
		for(Lxyybzzd one : page.getList()){
			String czyky = one.getCzysy();
			if("Y".equals(czyky)){
				czyky = "是";
			}else{
				czyky = "<font color='red'>否</font>";
			}
			table.add(new Tr(
					new Td(one.getBzbh()),
					new Td(one.getBzmc()),
					new Td(one.getLxsc()),
					new Td(czyky),
					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getCjsj())),
					new Td(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(one.getGxsj())),
					new Td("<a href='form?id="+one.getId()+"'>修改</a> &nbsp; "+ 
							"<a onclick=\"deleteData('"+one.getId()+"')\">删除</a>")
			));
		}
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", table.toString());*/
		return JSON.toJSONString(new AjaxRes(page));
	}
	
	/**
	 * 删除数据
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String deleteData(HttpServletRequest req){
		Lxyybzzd one = new Lxyybzzd();
		one.setId(req.getParameter("id"));
		one.setSybz("N");
		lxyybzzdService.editData(one, "id");
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 编辑数据
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req){
		String type = req.getParameter("czType");
		Lxyybzzd one = new Lxyybzzd();
		if("add".equals(type)){
			String id = UUID.randomUUID().toString().replace("-", "");
			//获得编号
			String bzbh = lxyybzzdService.getBzbhStr();
			one.setId(id);
			one.setBzbh(bzbh);
			one.setBzmc(req.getParameter("bzmc"));
			one.setLxsc(req.getParameter("lxsc"));
			one.setBzts("");
			one.setCzysy(req.getParameter("czyky"));
			lxyybzzdService.addData(one);
		}else{
			one.setId(req.getParameter("zbid"));
			one.setBzmc(req.getParameter("bzmc"));
			one.setLxsc(req.getParameter("lxsc"));
			one.setCzysy(req.getParameter("czyky"));
			one.setGxsj(new Date());
			lxyybzzdService.editData(one, "id");
		}
		return JSON.toJSONString(new AjaxRes());
	}
}
